<template>
  <div class="app-container">
    <!--table 搜索栏-->
    <table-search @onSearch="getUsers" @onRest="handleReset">
      <template slot="search">
        <el-input
          v-model="listQuery.id"
          placeholder="Id"
          clearable
          style="width: 120px;"
          class="filter-item"
          type="number"
          size="mini"
        />
        <el-select
          v-model="listQuery.status"
          placeholder="状态"
          clearable
          style="width: 90px"
          class="filter-item"
          size="mini"
        >
          <el-option v-for="(item,index) in userStatus" :key="item" :label="item" :value="index" />
        </el-select>
      </template>
      <template slot="button">
        <el-button
          class="filter-item"
          type="primary"
          size="mini"
          icon="el-icon-edit"
          @click="handleCreate"
        >添加</el-button>
      </template>
    </table-search>
    <!--table及分页-->
    <table-page :data="list" :loading="loadingTableVisible" :where="listQuery" :total="total">
      <template slot="column">
        <el-table-column label="编号" prop="id" align="center" width="80" />
        <el-table-column label="用户名" align="center">
          <template slot-scope="{row}">
            <a
              class="list-a"
              href="javascript:void(0)"
              @click="handleUserDialog(row)"
            >{{ row.userName }}</a>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="name" width="150" align="center" />
        <el-table-column label="角色">
          <template slot-scope="{row}">
            <span v-if="row.isDefault" style="color:#ff4949">系统默认超级管理员</span>
            <span v-else>{{$global.roles[row.roleId]}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" class-name="status-col" width="100">
          <template slot-scope="{row}">
            <span v-if="row.status==='normal'" style="color:#13ce66">
              <i class="el-icon-check" />
              {{ $enums["saaS.Model.Enums.UserStatus"][row.status] }}
            </span>
            <span v-if="row.status==='locking'" style="color:#ff4949">
              <svg-icon icon-class="lock" />
              {{ $enums["saaS.Model.Enums.UserStatus"][row.status] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="创建日期" width="150" align="center">
          <template slot-scope="scope">
            <div v-html="$htmlFunction.parseTime(scope.row.creationTime)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220" fixed="right">
          <template slot-scope="{row}">
            <el-button
              type="primary"
              class="list-mini-btn"
              v-if="!row.isDefault"
              @click="handleUpdate(row)"
            >编辑</el-button>
            <el-button
              type="danger"
              class="list-mini-btn"
              v-if="!row.isDefault"
              @click="handleDelete(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </template>
    </table-page>

    <!--明细-->
    <el-drawer
      class="user"
      :visible.sync="dialogUserVisible"
      direction="rtl"
      :show-close="false"
      :append-to-body="true"
      :destroy-on-close="true"
      @close="handleCloseDrawer"
    >
      <el-tabs @tab-click="handleTab">
        <el-tab-pane label="基础信息">
          <user-card v-if="this.tabStatus.isUserCard" :user="temp" />
        </el-tab-pane>
        <el-tab-pane label="功能权限">
          <permission-chart v-if="this.tabStatus.isPermissionChart" :user-id="temp.id" />
        </el-tab-pane>
        <el-tab-pane label="数据权限">
          <data-card v-if="this.tabStatus.isDataCard" :user-id="temp.id" />
        </el-tab-pane>
        <el-tab-pane label="日志轨迹">
          <timeline v-if="this.tabStatus.isTimeline" :user-id="temp.id" />
        </el-tab-pane>
      </el-tabs>
    </el-drawer>

    <!--创建与编辑-->
    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible" width="40%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 550px; margin-left:50px;"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="temp.userName" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="temp.roleId" clearable>
            <el-option
              v-for="(item,index) in this.$global.roles"
              :key="item.name"
              :label="item.name"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleDictionary } from "@/api/auth/role";
import UserCard from "../profile/components/UserCard";
import Timeline from "../profile/components/Timeline";
import DataCard from "./components/DataCard";
import PermissionChart from "./components/PermissionChart";
import TableSearch from "@/components/TableSearch";
import TablePage from "@/components/TablePage";
export default {
  components: {
    TablePage,
    UserCard,
    DataCard,
    Timeline,
    PermissionChart,
    TableSearch
  },
  data() {
    const listDefault = {
      page: 1,
      limit: 20,
      id: undefined,
      status: undefined,
      type: undefined
    };
    return {
      userStatus: null, // 用户状态
      list: null,
      total: 0,
      listQuery: listDefault,
      temp: {
        id: undefined,
        userName: "",
        name: "",
        roleId: undefined
      },
      tabStatus: {
        isUserCard: true,
        isPermissionChart: false,
        isDataCard: false,
        isTimeline: false
      },
      loadingTableVisible: true,
      dialogFormVisible: false, // 是否显示表单窗口
      dialogFormTitle: "",
      dialogAuthVisible: false,
      dialogAuthTitle: null,
      dialogUserVisible: false,
      rules: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.userStatus = this.$enums["saaS.Model.Enums.UserStatus"];
    this.getUsers();
  },
  methods: {
    // 获取用户列表
    getUsers() {
      this.loadingTableVisible = true;
      this.$form("/system/admin/list", this.listQuery)
        .then(response => {
          this.list = response.data.items;
          this.total = response.data.total;
          this.loadingTableVisible = false;
        })
        .catch(() => {
          this.loadingTableVisible = false;
        });
    },
    // 查看用户明细
    handleUserDialog(row) {
      const loading = this.$loading({ text: "正在加载数据..." });
      this.$get("/system/admin/info/" + row.id)
        .then(response => {
          loading.close();
          this.dialogUserVisible = true;
          this.temp = response.data;
        })
        .catch(error => {
          loading.close();
        });
    },
    // 重置数据模型
    resetTemp() {
      this.temp = {
        id: undefined,
        userName: "",
        name: "",
        roleId: undefined
      };
    },
    // 重置搜索
    handleReset() {
      this.listQuery = this.listDefault;
    },
    // 创建事件
    handleCreate() {
      this.resetTemp();
      this.dialogFormTitle = "创建用户";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    // 保存数据
    saveData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const loading = this.$loading({ text: "正在保存数据" });
          this.$form("/system/admin/save", this.temp)
            .then(response => {
              loading.close();
              this.dialogFormVisible = false;
              this.$message({
                message: response.message,
                type: response.success ? "success" : "error"
              });
              this.getUsers();
            })
            .catch(() => {
              loading.close();
            });
        }
      });
    },
    // 编辑事件
    handleUpdate(row) {
      const loading = this.$loading({ text: "正在加载数据..." });
      this.$get("/system/admin/info/" + row.id)
        .then(response => {
          loading.close();
          this.temp = response.data;
          this.dialogFormTitle = "编辑";
          this.dialogFormVisible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].clearValidate();
          });
        })
        .catch(error => {
          loading.close();
          console.error(error);
        });
    },
    // 删除事件
    handleDelete(row) {
      this.$confirm("确定删除用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const loading = this.$loading({ text: "正在删除数据" });
          deleteUserInfo(row.id)
            .then(response => {
              loading.close();
              this.$message({
                message: response.message,
                type: response.success ? "success" : "error"
              });
              this.getUsers();
            })
            .catch(() => {
              loading.close();
            });
        })
        .catch(err => {
          console.error(err);
        });
    },
    // 点击tab回调函数
    handleTab(action) {
      if (action.index === "1") {
        this.tabStatus.isPermissionChart = true;
      } else if (action.index === "2") {
        this.tabStatus.isDataCard = true;
      } else if (action.index === "3") {
        this.tabStatus.isTimeline = true;
      } else {
        this.tabStatus.isUserCard = true;
      }
    },
    // 关闭Drawr回调函数
    handleCloseDrawer() {
      this.tabStatus = {
        isUserCard: true,
        isPermissionChart: false,
        isDataCard: false,
        isTimeline: false
      };
    }
  }
};
</script>
<style >
.user /deep/.el-drawer__header {
  margin-bottom: 0px;
  padding: 0px;
}
.user /deep/ .el-tabs__nav-scroll {
  padding-left: 10px;
}
</style>
